package com.yanwuhuan.controller;

import com.github.pagehelper.PageInfo;
import com.yanwuhuan.controller.results.Code;
import com.yanwuhuan.controller.results.Result;
import com.yanwuhuan.domain.User;
import com.yanwuhuan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public Result save(User user){
        boolean flag=userService.save(user);
        return new Result(flag? Code.SAVE_OK:Code.SAVE_ERROR);
    }
    @PutMapping
    public Result update(User user){
        boolean flag=userService.update(user);
        return new Result(flag? Code.UPDATE_OK:Code.UPDATE_ERROR);
    }
    @DeleteMapping("/{uuid}")
    public Result delete(@PathVariable Integer uuid){
        boolean flag=userService.delete(uuid);
        return new Result(flag? Code.DELETE_OK:Code.DELETE_ERROR);
    }
    @GetMapping("/{uuid}")
    public Result get(@PathVariable Integer uuid){
        User user = userService.get(uuid);
        return new Result(null != user ? Code.GET_OK:Code.GET_ERROR);
    }
    @GetMapping("/{page}/{size}")
    public Result getAll(@PathVariable int page,@PathVariable int size){
        PageInfo<User> all = userService.getAll(page, size);
        return new Result(null != all ? Code.GET_OK:Code.GET_ERROR,all);
    }
    @PostMapping("/login")
    public Result login(String userName,String password){
        User login = userService.login(userName, password);
        return new Result(null != login ? Code.GET_OK:Code.GET_ERROR,login);
    }
}
